package logic

import (
	"context"
	"encoding/json"
	"zg4_month_exam/user/rpc/userclient"

	"zg4_month_exam/user/api/internal/svc"
	"zg4_month_exam/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogic {
	return &SearchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SearchLogic) Search(req *types.Searchrequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	conn, err := l.svcCtx.UserRpc.Search(context.Background(), &userclient.SearchRequest{
		Index:  req.Index,
		Search: req.Search,
		Page:   int64(req.Page),
		Choose: int64(req.Choose),
	})
	if err != nil {
		return &types.Response{
			Code: int(conn.Code),
			Msg:  conn.Msg,
			Data: nil,
		}, nil
	}
	var i interface{}
	json.Unmarshal(conn.Data, &i)
	return &types.Response{
		Code: int(conn.Code),
		Msg:  conn.Msg,
		Data: i,
	}, nil
}
